package middlewares

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

/**
 * @Description
 * @Author huran
 * @Date
 * @Param
 * @return
 **/

func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 允许的来源
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		// 允许的方法
		c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
		// 允许的头部
		c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, x-token, Authorization")
		// 允许凭证
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		// 允许的最长时间
		c.Writer.Header().Set("Access-Control-Max-Age", "86400")
		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
			return
		}
		c.Next()
	}
}
